package com.panda.web.mapper.commons;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.panda.web.domain.vo.commons.PushJob;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface PushJobMapper extends BaseMapper<PushJob> {
    @Select("select job_id from push_job where  openid=#{openid} ")
    List<Long> findByOpenid(String openid);
@Select("<select id=\"findByOpenidAndFy\" resultType=\"java.lang.Long\">\n" +
        "    SELECT id\n" +
        "    FROM your_table_name\n" +
        "    WHERE openid = #{openid}\n" +
        "    LIMIT #{offset}, #{pageSize}\n" +
        "</select>\n")
    List<Long> findByOpenidAndFy(@Param("openid") String openid, @Param("offset")int offset,@Param("pageSize") Integer pageSize);

    @Select("SELECT job_id, creation_time,selected_count FROM push_job WHERE openid = #{openid} AND (end_time > #{now} OR end_time = 0) ORDER BY creation_time DESC")
    List<PushJob> findValidPushJobsByOpenid(@Param("openid") String openid, @Param("now") Date now);
    @Update({
            "<script>",
            "UPDATE push_job SET selected_count = selected_count + 1, last_selected_time = NOW()",
            "WHERE job_id IN",
            "<foreach item='item' index='index' collection='collect' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "AND openid = #{openid}",
            "</script>"
    })
    void batchUpdateSelectionStatus(@Param("collect") List<Long> collect, @Param("openid") String openid);

    @Select("SELECT count(*) FROM push_job WHERE openid = #{openid} AND job_id=#{jobId}")
    int findByOpenidAndJobId(@Param("openid") String openid, @Param("jobId")Long jobId);
}
